package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.igas.bomp.facade.BompMchntManageService;
import cc.rengu.igas.bomp.facade.enums.BompAuditFlagEnum;
import cc.rengu.igas.bomp.facade.request.MchntWithdrawManageRequest;
import cc.rengu.igas.bomp.facade.response.MchntWithdrawManageResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.redp.bizimpl.enums.BompQueryTypeEnum;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpBMchntFeeInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpBMchntFeeInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpPrivilegeCheck;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntFeeInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpPrivilegeCheckService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-22
 */
@Service
public class UpmpBMchntFeeInfoServiceImpl extends ServiceImpl<UpmpBMchntFeeInfoMapper, UpmpBMchntFeeInfo> implements IUpmpBMchntFeeInfoService {
    private String oprMenuId = "20000030";
    private String oprMenuName = "商户管理:统一支付平台商户管理";
    private String oprBtnNamePrefix = "统一支付平台商户管理";
    private String webNextUrl = "/admin/mcmp/bMchntBaseInfo/edit-tabs";

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public OperationResult mchntWithdrawManage(String instId, MchntWithdrawManageRequest req, BompMchntManageService bompMchntManageService,
                                               IUpmpPrivilegeCheckService authPrivilegeCheckService, String traceNo, String optFlag) throws RedpException {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        req.setHeader(CommonUtils.packageBompHeader(traceNo,BompTxnNumEnum.mchntWithdrawManage.getType(),instId, null));
        req.setOprType("1");
        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(optFlag)){
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(),
                    oprBtnNamePrefix+BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT+PageOptFlagEnum.OPT_FLAG_ADD.getDesc());
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                req.setChecked(BompAuditFlagEnum.CHECK.getType());
                req.setOprMenuId(oprMenuId);
                req.setOprMenuName(oprMenuName);
                req.setWebNextUrl(webNextUrl);

                //设置主记录是否需要显示

            }else {
                req.setChecked(BompAuditFlagEnum.NO_CHECK.getType());
            }
        }else if(PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(optFlag)){

            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(),
                    oprBtnNamePrefix+BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT+PageOptFlagEnum.OPT_FLAG_UPDATE.getDesc());

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    req.setChecked(BompAuditFlagEnum.CHECK.getType());
                    req.setOprMenuId(oprMenuId);
                    req.setOprMenuName(oprMenuName);
                    req.setWebNextUrl(webNextUrl);
                    //设置主记录是否需要显示

                }else {
                    req.setChecked(BompAuditFlagEnum.NO_CHECK.getType());
                }
            }
        }


        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        try{

         log.trace("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+ BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易请求报文：" + JSON.toJSONString(req));

            Result<MchntWithdrawManageResponse> response= bompMchntManageService.mchntWithdrawManage(req);

         log.trace("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易返回报文：" + JSON.toJSONString(response));
            if(response.isSuccess()){
                MchntWithdrawManageResponse resp = response.getResult();
                // 返回码：00-进件成功，其他均视为失败
                if (BusinessContainer.BSPS_SUCCESS.equals(resp.getRespCode())) {
                    result.put("respCode", resp.getRespCode());
                    result.put("respMsg", resp.getRespDesc());
                 log.trace("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易成功");
                    return OperationResult.buildSuccessResult(BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易成功",result);
                }else {
                    if (resp.getRespCode().equals("ER12BM64")) {
                        return OperationResult.buildFailureResult(resp.getRespDesc(), result);
                    }
                    result.put("respCode", resp.getRespCode());
                    result.put("respMsg", resp.getRespDesc());
                    return OperationResult.buildFailureResult(BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易失败",result);
                }
            }else{
                result.put("resCode", BusinessContainer.ERROR);
                result.put("resMsg", BompTxnNumEnum.mchntWithdrawManage.getDesc()+"没有获得返回报文");
                log.error("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易没有获得返回信息");
                return OperationResult.buildFailureResult(BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易失败",result);
            }
        }catch (Exception e) {
            // TODO: handle exception
            result.put("resCode", BusinessContainer.ERROR);
            result.put("resMsg", e.getMessage());
            log.error("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易出现异常，异常信息："+e.getMessage(),e);
//            throw new RedpException("商户编号为：" + req.getMchntNo() + "的商户，"+BompTxnNumEnum.mchntProdInfoManage.getDesc()+"交易出现异常",e);
            return OperationResult.buildFailureResult(BompTxnNumEnum.mchntWithdrawManage.getDesc()+"交易失败",result);
        }
    }
    @Override
    public List<UpmpBMchntFeeInfo> getfee(String feeCd){
        LambdaQueryWrapper<UpmpBMchntFeeInfo> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(UpmpBMchntFeeInfo::getFeeCode,feeCd);
        wrapper.eq(UpmpBMchntFeeInfo::getRecordStatus,"Y");
        return this.list(wrapper);
    }
}
